<template>
  <div class='goods-relevant'>
    <header class="header">
      <i class="icon" />
      <span class="title"> {{ goodsId ? '同类商品推荐' : '猜你喜欢' }}</span>
    </header>
    <!-- 商品推荐轮播图部分 -->
    <XtxCarousel :sliders="sliders" style="height:380px" auto-play></XtxCarousel>
  </div>
</template>
<script lang="ts" setup>
import { findRelGoods } from '../../api/goods'
import { ref } from 'vue'
import XtxCarousel from '../../components/library/xtx-carousel.vue'
const props = defineProps({
  // 拿到该商品的Id
  goodsId: {
    type: String,
    default: undefined
  }
})
// 获取商品推荐数据
const sliders = ref([])
findRelGoods(props.goodsId).then(({ result }) => {
  // 拿到数据对它们每页4条进行分页
  const size = 4
  const total = Math.ceil(result.length / size)
  for (let i = 0; i < total; i++) {
    // 每一段截取数组中的4个数据, 依次往下
    sliders.value.push(result.slice(i * size, (i + 1) * size))
  }
})
</script>
<style  lang='less' scoped>
@import '../../assets/styles/common.less';
@import '../../assets/styles/variables.less';

.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;

  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;

    .title {
      font-size: 20px;
      margin-left: 10px;
    }

    .icon {
      display: inline-block;
      position: relative;
      width: 16px;
      height: 16px;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      transform: rotate(45deg);

      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}

:deep(.xtx-carousel) {
  height: 380px;

  .carousel {
    &-indicator {
      bottom: 30px;

      span {
        &.active {
          background: @xtxColor;
        }
      }
    }

    &-btn {
      top: 110px;
      opacity: 1;
      background: rgba(0, 0, 0, 0);
      color: #ddd;

      i {
        font-size: 30px;
      }
    }
  }
}
</style>
